{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 导入本项目所需要的包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import datasets\n",
    "\n",
    "from sklearn import svm\n",
    "\n",
    "# 使用交叉验证的方法，把数据集分为训练集合测试集\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The competition datafiles are in the directory ../input\n",
    "# 加载数据集\n",
    "def load_data():\n",
    "    dataset = pd.read_csv(\"~/Desktop/knn/input/train.csv\")\n",
    "    label = dataset.values[:,0]\n",
    "    train = dataset.values[:,1:]\n",
    "    testdata  = pd.read_csv(\"~/Desktop/knn/input/test.csv\").values\n",
    "    return label,train,testdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 使用LinearSVC考察线性分类SVM的预测能力\n",
    "def test_LinearSVC(label,train,testdata):\n",
    "    \n",
    "    # 选择模型\n",
    "    cls = svm.LinearSVC()\n",
    "    \n",
    "    # 把数据交给模型训练\n",
    "    cls.fit(train,label)\n",
    "    \n",
    "    # 预测数据\n",
    "    #print(cls.predict(testdata))\n",
    "    results=cls.predict(testdata)\n",
    "    return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__==\"__main__\":\n",
    "    label,train,testdata = load_data()\n",
    "    result = test_LinearSVC(label,train,testdata)\n",
    "    pd.DataFrame({\"ImageId\": list(range(1,len(testdata)+1)),\"Label\": result}).to_csv(\n",
    "    '~/Desktop/knn/output/Digit_recogniser_SVM_LinearSVC.csv', index=False,header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
